home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / iscannum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  8.2 KB  |  353 lines

  1. /* Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* iscannum.c */
  20. /* Number scanner for Ghostscript interpreter */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "scommon.h"
  25. #include "iscannum.h"            /* defines interface */
  26. #include "scanchar.h"
  27. #include "store.h"
  28.  
  29. #define is_digit(d, c)\
  30.   ((d = decoder[c]) < 10)
  31.  
  32. #define scan_sign(sign, ptr)\
  33.   switch ( *ptr ) {\
  34.     case '-': sign = -1; ptr++; break;\
  35.     case '+': sign = 1; ptr++; break;\
  36.     default: sign = 0;\
  37.   }
  38.  
  39. /* Note that the number scanning procedures use a byte ** and a byte * */
  40. /* rather than a stream.  (It makes quite a difference in performance.) */
  41. #define ngetc(cvar, sp, exit)\
  42.   if ( sp >= end ) { exit; } else cvar = *sp++
  43.  
  44. /* Scan a number.  If the number consumes the entire string, return 0; */
  45. /* if not, set *psp to the first character beyond the number and return 1. */
  46. int
  47. scan_number(register const byte *sp, const byte *end, int sign,
  48.   ref *pref, const byte **psp)
  49. {    /* Powers of 10 up to 6 can be represented accurately as */
  50.     /* a single-precision float. */
  51. #define num_powers_10 6
  52.     static const float powers_10[num_powers_10+1] =
  53.     {    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6    };
  54.     static const double neg_powers_10[num_powers_10+1] =
  55.     {    1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6    };
  56.     int ival;
  57.     long lval;
  58.     double dval;
  59.     int exp10;
  60.     int code = 0;
  61.     register int c, d;
  62.     register const byte _ds *decoder = scan_char_decoder;
  63.     ngetc(c, sp, return_error(e_syntaxerror));
  64. #define would_overflow(val, d, maxv)\
  65.   (val >= maxv / 10 && (val > maxv / 10 || d > (int)(maxv % 10)))
  66.     if ( !is_digit(d, c) )
  67.     {    if ( c != '.' )
  68.             return_error(e_syntaxerror);
  69.         /* Might be a number starting with '.'. */
  70.         ngetc(c, sp, return_error(e_syntaxerror));
  71.         if ( !is_digit(d, c) )
  72.             return_error(e_syntaxerror);
  73.         ival = 0;
  74.         goto i2r;
  75.     }
  76.  
  77.     /* Accumulate an integer in ival. */
  78.     /* Do up to 4 digits without a loop, */
  79.     /* since we know this can't overflow and since */
  80.     /* most numbers have 4 (integer) digits or fewer. */
  81.     ival = d;
  82.     if ( end - sp >= 3 )        /* just check once */
  83.     {    if ( !is_digit(d, (c = *sp)) )
  84.         {    sp++;
  85.             goto ind;
  86.         }
  87.         ival = ival * 10 + d;
  88.         if ( !is_digit(d, (c = sp[1])) )
  89.         {    sp += 2;
  90.             goto ind;
  91.         }
  92.         ival = ival * 10 + d;
  93.         sp += 3;
  94.         if ( !is_digit(d, (c = sp[-1])) )
  95.             goto ind;
  96.         ival = ival * 10 + d;
  97.     }
  98.     for ( ; ; ival = ival * 10 + d )
  99.     {    ngetc(c, sp, goto iret);
  100.         if ( !is_digit(d, c) )
  101.             break;
  102.         if ( would_overflow(ival, d, max_int) )
  103.             goto i2l;
  104.     }
  105. ind:    switch ( c )
  106.     {
  107.     case '.':
  108.         ngetc(c, sp, c = EOFC);
  109.         goto i2r;
  110.     default:
  111.         *psp = sp;
  112.         code = 1;
  113.         break;
  114.     case 'e': case 'E':
  115.         if ( sign < 0 )
  116.             ival = -ival;
  117.         dval = ival;
  118.         exp10 = 0;
  119.         goto fe;
  120.     case '#':
  121.     {    ulong uval = 0, lmax;
  122. #define radix (uint)ival
  123.         if ( sign || radix < min_radix || radix > max_radix )
  124.             return_error(e_syntaxerror);
  125.         /* Avoid multiplies for power-of-2 radix. */
  126.         if ( !(radix & (radix - 1)) )
  127.         {    int shift;
  128.             switch ( radix )
  129.             {
  130. #define set_shift(n)\
  131.   shift = n; lmax = max_ulong >> n
  132.             case 2: set_shift(1); break;
  133.             case 4: set_shift(2); break;
  134.             case 8: set_shift(3); break;
  135.             case 16: set_shift(4); break;
  136.             case 32: set_shift(5); break;
  137. #undef set_shift
  138.             default:        /* can't happen */
  139.               return_error(e_rangecheck);
  140.             }
  141.             for ( ; ; uval = (uval << shift) + d )
  142.             {    ngetc(c, sp, break);
  143.                 d = decoder[c];
  144.                 if ( d >= radix )
  145.                 {    *psp = sp;
  146.                     code = 1;
  147.                     break;
  148.                 }
  149.                 if ( uval > lmax )
  150.                     return_error(e_limitcheck);
  151.             }
  152.         }
  153.         else
  154.         {    int lrem = max_ulong % radix;
  155.             lmax = max_ulong / radix;
  156.             for ( ; ; uval = uval * radix + d )
  157.             {    ngetc(c, sp, break);
  158.                 d = decoder[c];
  159.                 if ( d >= radix )
  160.                 {    *psp = sp;
  161.                     code = 1;
  162.                     break;
  163.                 }
  164.                 if ( uval >= lmax &&
  165.                      (uval > lmax || d > lrem)
  166.                    )
  167.                     return_error(e_limitcheck);
  168.             }
  169.         }
  170. #undef radix
  171.         make_int_new(pref, uval);
  172.         return code;
  173.     }
  174.     }
  175. iret:    make_int_new(pref, (sign < 0 ? -ival : ival));
  176.     return code;
  177.  
  178.     /* Accumulate a long in lval. */
  179. i2l:    for ( lval = ival; ; )
  180.     {    if ( would_overflow(lval, d, max_long) )
  181.         {    /* Make a special check for entering the smallest */
  182.             /* (most negative) integer. */
  183.             if ( lval == max_long / 10 &&
  184.                  d == (int)(max_long % 10) + 1 && sign < 0
  185.                )
  186.             {    ngetc(c, sp, c = EOFC);
  187.                 dval = -(double)min_long;
  188.                 if ( c == 'e' || c == 'E' || c == '.' )
  189.                 {    exp10 = 0;
  190.                     goto fs;
  191.                 }
  192.                 else if ( !is_digit(d, c) )
  193.                 {    lval = min_long;
  194.                     break;
  195.                 }
  196.             }
  197.             else
  198.                 dval = lval;
  199.             goto l2d;
  200.         }
  201.         lval = lval * 10 + d;
  202.         ngetc(c, sp, goto lret);
  203.         if ( !is_digit(d, c) )
  204.             break;
  205.     }
  206.     switch ( c )
  207.     {
  208.     case '.':
  209.         ngetc(c, sp, c = EOFC);
  210.         exp10 = 0;
  211.         goto l2r;
  212.     default:
  213.         *psp = sp;
  214.         code = 1;
  215.         break;
  216.     case 'e': case 'E':
  217.         exp10 = 0;
  218.         goto le;
  219.     case '#':
  220.         return_error(e_syntaxerror);
  221.     }
  222. lret:    make_int_new(pref, (sign < 0 ? -lval : lval));
  223.     return code;
  224.  
  225.     /* Accumulate a double in dval. */
  226. l2d:    exp10 = 0;
  227.     for ( ; ; )
  228.     {    dval = dval * 10 + d;
  229.         ngetc(c, sp, c = EOFC);
  230.         if ( !is_digit(d, c) )
  231.             goto fs;
  232.     }
  233.  
  234.     /* We saw a '.' while accumulating an integer in ival. */
  235. i2r:    exp10 = 0;
  236.     while ( is_digit(d, c) )
  237.     {    if ( would_overflow(ival, d, max_int) )
  238.         {    lval = ival;
  239.             goto l2r;
  240.         }
  241.         ival = ival * 10 + d;
  242.         exp10--;
  243.         ngetc(c, sp, c = EOFC);
  244.     }
  245.     if ( sign < 0 )
  246.         ival = -ival;
  247.     /* Take a shortcut for the common case */
  248.     if ( !(c == 'e' || c == 'E' || exp10 < -num_powers_10) )
  249.     {    /* Check for trailing garbage */
  250.         if ( c != EOFC )
  251.             *psp = sp, code = 1;
  252.         make_real_new(pref, ival * neg_powers_10[-exp10]);
  253.         return code;
  254.     }
  255.     dval = ival;
  256.     goto fe;
  257.  
  258.     /* We saw a '.' while accumulating a long in lval. */
  259. l2r:    while ( is_digit(d, c) )
  260.     {    if ( would_overflow(lval, d, max_long) )
  261.         {    dval = lval;
  262.             goto fd;
  263.         }
  264.         lval = lval * 10 + d;
  265.         exp10--;
  266.         ngetc(c, sp, c = EOFC);
  267.     }
  268. le:    if ( sign < 0 )
  269.         lval = -lval;
  270.     dval = lval;
  271.     goto fe;
  272.  
  273.     /* Now we are accumulating a double in dval. */
  274. fd:    while ( is_digit(d, c) )
  275.     {    dval = dval * 10 + d;
  276.         exp10--;
  277.         ngetc(c, sp, c = EOFC);
  278.     }
  279. fs:    if ( sign < 0 )
  280.         dval = -dval;
  281. fe:    /* Now dval contains the value, negated if necessary. */
  282.     switch ( c )
  283.     {
  284.     case 'e': case 'E':
  285.     {    /* Check for a following exponent. */
  286.         int esign = 0;
  287.         int iexp;
  288.         ngetc(c, sp, return_error(e_syntaxerror));
  289.         switch ( c )
  290.         {
  291.         case '-':
  292.             esign = 1;
  293.         case '+':
  294.             ngetc(c, sp, return_error(e_syntaxerror));
  295.         }
  296.         /* Scan the exponent.  We limit it arbitrarily to 999. */
  297.         if ( !is_digit(d, c) )
  298.             return_error(e_syntaxerror);
  299.         iexp = d;
  300.         for ( ; ; iexp = iexp * 10 + d )
  301.         {    ngetc(c, sp, break);
  302.             if ( !is_digit(d, c) )
  303.             {    *psp = sp;
  304.                 code = 1;
  305.                 break;
  306.             }
  307.             if ( iexp > 99 )
  308.                 return_error(e_limitcheck);
  309.         }
  310.         if ( esign )
  311.             exp10 -= iexp;
  312.         else
  313.             exp10 += iexp;
  314.         break;
  315.     }
  316.     default:
  317.         *psp = sp;
  318.         code = 1;
  319.     case EOFC:
  320.         ;
  321.     }
  322.     /* Compute dval * 10^exp10. */
  323.     if ( exp10 > 0 )
  324.     {    while ( exp10 > num_powers_10 )
  325.             dval *= powers_10[num_powers_10],
  326.             exp10 -= num_powers_10;
  327.         if ( exp10 > 0 )
  328.             dval *= powers_10[exp10];
  329.     }
  330.     else if ( exp10 < 0 )
  331.     {    while ( exp10 < -num_powers_10 )
  332.             dval /= powers_10[num_powers_10],
  333.             exp10 += num_powers_10;
  334.         if ( exp10 < 0 )
  335.             dval /= powers_10[-exp10];
  336.     }
  337.     /*
  338.      * Check for an out-of-range result.  Currently we don't check for
  339.      * absurdly large numbers of digits in the accumulation loops,
  340.      * but we should.
  341.      */
  342.     if ( dval >= 0 )
  343.       { if ( dval > MAX_FLOAT )
  344.           return_error(e_limitcheck);
  345.       }
  346.     else
  347.       { if ( dval < -MAX_FLOAT )
  348.           return_error(e_limitcheck);
  349.       } 
  350.     make_real_new(pref, dval);
  351.     return code;
  352. }
  353.